'''
 * CaRComp Execution Environment
 * 
 * Copyright (C) 2011 Taylor L. Riche
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
'''


class SizeMismatchError(Exception):
    pass

class BadStringValError(Exception):
    pass

class BitMap(object):
    '''
    classdocs
    '''

    def __init__(self, size, string_val=None):
        '''
        Constructor
        '''
        self._core = [False for i in range(0, size)]
        if string_val is not None:
            if len(string_val) > size:
                raise SizeMismatchError
            else:
                index = 0
                for bit in string_val:
                    if bit == '1':
                        self._core[index] = True
                    elif bit != '0':
                        raise BadStringValError
                    index += 1
                        
        
    def set(self, index):
        self._core[index] = True
        
    def isSet(self, index):
        return self._core[index]
    
    def serialize(self):
        ret_val = ''
        for bit in self._core:
            if bit is True:
                ret_val += '1'
            else:
                ret_val += '0'
        return ret_val
    
